<template>
  <div class="login" id="login">
    <div class="login-div">
      <div class="left-img">
        <img
          src="@/assets/login_images/tupian.png"
          style="height: 70%; width: 90%"
        />
      </div>
      <div class="right">
        <div class="right-title">绩效管理平台</div>
        <div style="margin: 20px 15%">
          <a-row type="flex" class="arow">
            <a-col flex="50px" class="acol-left">
              <img
                src="@/assets/login_images/ic-zhanghao.png"
                class="img-left"
              />
            </a-col>
            <a-col flex="auto"
              ><a-input
                placeholder="请输入账号"
                class="ainput"
                v-model:value="ApiForm.username"
            /></a-col>
          </a-row>

          <a-row type="flex" class="arow" style="margin-top: 20px">
            <a-col flex="50px" class="acol-left">
              <img
                src="@/assets/login_images/ic-mima.png"
                class="img-left"
                style="width: 23px; height: 23px"
              />
            </a-col>
            <a-col flex="auto"
              ><a-input-password
                placeholder="请输入密码"
                v-model:value="ApiForm.password"
                style="width: 95%; color: #666666; font-size: 18px"
            /></a-col>
          </a-row>

          <a-row :gutter="16" style="margin-top: 20px">
            <a-col class="gutter-row" :span="17">
              <a-row type="flex" class="arow">
                <a-col flex="50px" class="acol-left">
                  <img
                    src="@/assets/login_images/ic-anquan.png"
                    class="img-left"
                  />
                </a-col>
                <a-col flex="auto"
                  ><a-input
                    placeholder="请输入验证码"
                    class="ainput"
                    v-model:value="ApiForm.captcha"
                /></a-col>
              </a-row>
            </a-col>
            <a-col
              class="gutter-row"
              :span="6"
              id="captcha"
              style="cursor: pointer"
              @click="refreshCode"
            >
              <security
                :identifyCode="identifyCode"
                :contentWidth="contentWidth"
                :contentHeight="50"
                v-if="showCaptcha"
              ></security>
            </a-col>
          </a-row>

          <a-button
            type="primary"
            :block="true"
            shape="round"
            style="margin-top: 100px"
            size="large"
            @click="submit"
          >
            登录
          </a-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { IdentifyCodes, CaptchaLength } from "@/config";
import security from "@/plugins/components/catch.vue";

export default {
  components: {
    security,
  },
  data() {
    return {
      identifyCode: "",
      // 验证码 宽度
      contentWidth: 0,
      // 验证码 控件
      showCaptcha: false,
      // Api表单
      ApiForm: {
        username: "admin",
        password: "admin",
        captcha: "",
      },
    };
  },
  mounted() {
    this.identifyCode = "";
    this.makeCode(IdentifyCodes, CaptchaLength);
    /**
     * 获取验证码模板宽度   动态获取宽度
     *  -首先获取到验证码所在容器的 宽度  这里获取宽度一定要在mounted钩子函数里面获取 因为这个函数执行的时候dom节点已经完全加载，created不行
     *  -利用v-if 先使得界面先加载其余数据，等window获取到都容器宽度后再渲染验证码模块
     */
    var captcha = document.getElementById("captcha");
    var width = window.getComputedStyle(captcha, null).width;
    this.contentWidth = width.slice(0, width.indexOf("p"));
    this.showCaptcha = true;
  },
  methods: {
    // 验证码处理
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
    // 刷新验证码
    refreshCode() {
      this.identifyCode = "";
      this.makeCode(IdentifyCodes, CaptchaLength);
    },
    // 获取验证码
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode +=
          IdentifyCodes[this.randomNum(0, IdentifyCodes.length)];
      }
      // 验证码
      this.ApiForm.captcha = this.identifyCode;
      console.log(this.identifyCode);
    },
    /**
     * 提交表单
     */
    submit() {
      if (this.ApiForm.username == "" || this.ApiForm.username == null) {
        this.$message({
          message: "请填写您的账号哦",
          type: "error",
          center: true,
        });
        return;
      }
      if (this.ApiForm.password == "" || this.ApiForm.password == null) {
        this.$message({
          message: "请填写您的密码哦",
          type: "error",
          center: true,
        });
        return;
      }
      if (this.ApiForm.captcha == "" || this.ApiForm.captcha == null) {
        this.$message({
          message: "请填写验证码哦",
          type: "error",
          center: true,
        });
        return;
      }
      // 全部通过
      this.$router.push("/");
    },
  },
};
</script>

<style lang="less" scoped>
.gutter-box {
  background: #00a0e9;
  height: 25px;
}
.login {
  height: 100vh;
  width: 100vw;
  background-size: 100% 100%;
  background-image: url("../../assets/login_images/bg_image.png");
  display: flex;
  justify-content: center;
  align-items: center;
  &-div {
    width: 70vw;
    height: 70vh;
    background-color: #fff;
    border-radius: 16px;
    display: flex;
    & > div {
      flex: 1;
      height: 100%;
    }
    .left-img {
      display: flex;
      justify-content: center;
      align-items: center;
      border-top-left-radius: 16px;
      border-bottom-left-radius: 16px;
      background-image: linear-gradient(#00cda6, #007c6c);
    }
    .right {
      text-align: center;
      padding: 20px 0;
      display: inline-block;
      &-title {
        font-size: 32px;
        color: #333333;
        font-weight: Medium;
        padding: 70px 0;
        font-family: Noto Sans S Chinese;
      }
      .img-left {
        height: 26px;
        width: 26px;
        position: relative;
        top: -2px;
      }
    }
  }
  .not-see {
    font-size: 10px;
    height: 50px;
    line-height: 50px;
    color: #006e6d;
    cursor: pointer;
  }
  .acol-left {
    overflow-x: hidden;
  }
  .acol-left::after {
    content: "";
    position: absolute;
    right: 0px;
    top: 17.5px;
    height: 15px;
    width: 1.5px;
    background-color: #00796a;
  }
  .arow {
    height: 25px;
    background-color: #f5f5f5;
    height: 50px;
    line-height: 50px;
    border-radius: 10px;
  }
  .ainput {
    height: 95%;
    width: 95%;
    color: #666666;
    font-size: 14px;
  }
  @media only screen and (max-width: 1024px) {
    .login-div {
      width: 90vw;
      height: 70vh;
    }
    .left-img {
      display: none;
    }
  }
}
</style>